#include "fString.h"
#include <iostream>



fString::fString()
{
	m_stringLocation = nullptr;
	//m_stringLocation = new char[1];
	//m_stringLocation[0] = '\0';
}

fString::fString(const char* stringLocation)
{
	int temp = 0;
	
	for(int i=0;;i++)
	{
		if(!(stringLocation[i] == '\0'))
		{
			temp++;
			stringLocation[i];
		}
		else
		{
			break;
		}
	}

	m_stringLocation = new char[temp+1];
	
	for(int i=0;i < (temp+1);i++)
	{
		m_stringLocation[i] = stringLocation[i];
	}

}

fString::fString(const fString & fs)
{
	m_stringLocation = new char[strlen(fs.m_stringLocation)+1];
	strcpy(m_stringLocation, fs.m_stringLocation);
}

fString::~fString()
{
	delete [] m_stringLocation;
}

void fString::show() const
{
	std::cout << m_stringLocation << std::endl;
}

void fString::put(const char* stringLocation)
{
	delete m_stringLocation;

	int temp = 0;
	
	for(int i=0;;i++)
	{
		if(!(stringLocation[i] == '\0'))
		{
			temp++;
			stringLocation[i];
		}
		else
		{
			break;
		}
	}

	m_stringLocation = new char[temp+1];
	
	for(int i=0;i < temp+1;i++)
	{
		m_stringLocation[i] = stringLocation[i];
	}
}

fString & fString::operator=(const fString & fs)
{
	delete [] m_stringLocation;

	m_stringLocation = new char[strlen(fs.m_stringLocation)+1];
	strcpy(m_stringLocation, fs.m_stringLocation);
	return *this;
}

const fString fString::operator+(const fString & rfs) const
{
	fString temp;

	temp.m_stringLocation = new char[strlen(rfs.m_stringLocation)+strlen(m_stringLocation)+1];
	strcpy(temp.m_stringLocation, m_stringLocation);
	strcat(temp.m_stringLocation, rfs.m_stringLocation);

	return temp;
}

const fString fString::operator+(const char* pc) const
{
	fString temp;

	temp.m_stringLocation = new char[strlen(pc)+strlen(m_stringLocation)+1];
	strcpy(temp.m_stringLocation, m_stringLocation);
	strcat(temp.m_stringLocation, pc);

	return temp;
}

fString::operator char*() const
{
	
	static char temp[MAXSIZE];
	char * pc;

	//This stores the string in the static storage.
	pc = new(temp) char[strlen(m_stringLocation)+1];
	strcpy(temp, m_stringLocation);
	
	return pc;
}

std::ostream & operator<<(std::ostream & os, fString & fs)
{
	os << fs.m_stringLocation << std::endl;
	return os;
}

const fString operator+(const char* pc, fString fs)
{
	fString temp;

	temp.m_stringLocation = new char[strlen(pc)+strlen(fs.m_stringLocation)+1];
	strcpy(temp.m_stringLocation, pc);
	strcat(temp.m_stringLocation, fs.m_stringLocation);

	return temp;
}

